package com.github.tvbox.osc.ui.activity;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.AbsCallback;
import com.lzy.okgo.model.Response;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.lifecycle.MutableLiveData;

/**
 * @ClassName: RecWordHelper
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @author: huangrongcai
 * @date: 2024/11/10
 * @Copyright: 黄嵘才（HuangRc)
 */
public class RecWordHelper {

    public static void loadRecWord(final String keyword, MutableLiveData<List<String>> queryWord) {
        //1联想词
        //2热点词
        String url = "https://node.video.qq.com/x/api/hot_mobilesearch";
        Map<String, String> params = new HashMap<>();
        if (keyword == null || keyword.length() == 0) {
            url = "https://node.video.qq.com/x/api/hot_mobilesearch";
            params.put("url", url);
            params.put("channdlId", "0");
            params.put("_", System.currentTimeMillis() + "");
        } else {
            url = "https://s.video.qq.com/smartbox";
            params.put("url", url);
            params.put("plat", 2 + "");
            params.put("ver", 0 + "");
            params.put("num", 10 + "");
            params.put("otype", "json");

        }

        params.put("query", keyword);
        OkGo.<String>get(url)
                .params(params)
                .execute(new AbsCallback<String>() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        try {
                            ArrayList<String> hots = new ArrayList<>();
                            String result = response.body();
                            if (keyword == null || keyword.length() == 0) {
                                JsonArray itemList = JsonParser.parseString(response.body()).getAsJsonObject().get("data").getAsJsonObject().get("itemList").getAsJsonArray();
                                for (JsonElement ele : itemList) {
                                    JsonObject obj = (JsonObject) ele;
                                    hots.add(obj.get("title").getAsString().trim().replaceAll("<|>|《|》|-", "").split(" ")[0]);
                                }
                                queryWord.postValue(hots);//
                            } else {
                                JsonObject json = JsonParser.parseString(result.substring(result.indexOf("{"), result.lastIndexOf("}") + 1)).getAsJsonObject();
                                JsonArray itemList = json.get("item").getAsJsonArray();
                                for (JsonElement ele : itemList) {
                                    JsonObject obj = (JsonObject) ele;
                                    hots.add(obj.get("word").getAsString().trim());
                                }
                                queryWord.postValue(hots);//
                            }

                        } catch (Throwable th) {
                            th.printStackTrace();
                        }
                    }

                    @Override
                    public String convertResponse(okhttp3.Response response) throws Throwable {
                        return response.body().string();
                    }
                });
    }
}
